#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>

#define NN 64

void* RecvData(void* arg)
{
    char textname[NN]={0};
    char buf[NN]={0};
    char sizebuf[4]={0};
    char namelen[NN]={0};
    int size=0;
    int len=0;
    int* c_socket=(int*)arg;
    int client_socket=*c_socket;
    free(c_socket);

    while (1)
    {
        recv(client_socket,namelen,4,0);//接收文件名长度
        len=*(int*)namelen;

        recv(client_socket,textname,len,0);//接收文件名
        recv(client_socket,sizebuf,4,0);//接收文件长度
        size=*(int*)sizebuf;
        
        printf("textname:%s\n",textname);
        printf("size:%d\n",size);

        int fd=open(textname,O_WRONLY|O_CREAT|O_TRUNC,0666);//打开文件进行操作
        if(fd==-1)
        {
            perror("open err\n");
            close(client_socket);
            return NULL;
        }

        int ret =0;
        while (1)
        {
            ret=recv(client_socket,buf,size,0);//接收文件
            if(ret<0)
            {
                perror("recv err\n");
                break;
            }
            else if(ret>0)
            {
                write(fd,buf,size);
                if (ret<NN)
                {
                    printf("recv success\n");
                    send(client_socket,"recv success\n",64,0);//发送接收成功
                    break;
                }
            }
            else
            {
                printf("peer quit\n");
                close(client_socket);
                exit(0);
            }
        }
        close(fd);
        
    }
    close(client_socket);
    return NULL;
}

// 用法：./server
int main(int argc, char const* argv[])
{
    int server_socket;
    server_socket = socket(AF_INET,SOCK_STREAM,0);
    if (server_socket==-1)
    {
        perror("socket err\n");
        return -1;
    }
    
    struct sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_port=htons(8888);
    addr.sin_addr.s_addr=INADDR_ANY;

    if(bind(server_socket,(struct sockaddr*)&addr,sizeof(addr))==-1)
    {
        perror("bind err\n");
        close(server_socket);
        return -1;
    }

         if (listen(server_socket,5)==-1)
        {
            perror("listen err\n");
            close(server_socket);
            return -1;
        }
        printf("等待接收\n");

        struct sockaddr_in client_addr;
        socklen_t client_addr_len = sizeof(client_addr);        
        while (1)
        {
            int client_socket=-1;
            client_socket=accept(server_socket,(struct sockaddr*)&client_addr,&client_addr_len);

            printf("ip = %s\n",inet_ntoa(client_addr.sin_addr));

            if (client_socket==-1)
            {
                perror("accept err\n");
                close(server_socket);
                return -1;
            }

            pthread_t thread;
            int *c_socket=(int*)malloc(sizeof(int));
            *c_socket=client_socket;
            if(pthread_create(&thread,NULL,RecvData,(void*)c_socket) != 0)
            {
                perror("create thread error\n");
                close(server_socket);
                return -1;
            }
            pthread_detach(thread);
        }
        
        
    close(server_socket);
    while (1);
    
    return 0;
}